<template>
  <el-dialog :title="isEdit?'修改标签':'新增标签'" :visible.sync="tagsShow" width="20%" :before-close="cancelFn">

    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-width="80px"
      class="diagForm"
    >
      <el-form-item label="所属学科" prop="subjectID">
        <el-select
          v-model="form.subjectID"
          placeholder="请选择"
          class="selectForm"
        >
          <el-option
            v-for="obj in subjectlist"
            :key="obj.value"
            :label="obj.label"
            :value="obj.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="目录名称" prop="tagName">
        <el-input
          v-model="form.tagName"
          placeholder="请输入目录名称"
          class="selectForm"
        />
      </el-form-item>

      <el-form-item class="btnFrom">
        <el-button @click="cancelFn">取消</el-button>
        <el-button type="primary" @click="addTagsFn">确认</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { simple as subjectList } from '@/api/hmmm/subjects'
import { add as addTags, detail, update as updateTags } from '@/api/hmmm/tags'
export default {
  name: 'EditAddTags',
  props: {
    tagsShow: {
      type: Boolean,
      required: true
    },
    id: {
      type: [String, Number],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        subjectID: [{ required: true, message: '请选择', trigger: 'change' }],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      },
      subjectlist: []
    }
  },
  created() {
    // 调用学科列表
    this.subjectList()

    // 标签回显
    this.tagEditshow()
  },
  methods: {
    // 标签回显
    async tagEditshow() {
      if (this.isEdit) {
        const res = await detail({ id: this.id })

        for (const k in res.data) {
          if (this.form[k] === '') {
            this.form[k] = res.data[k]
          }
        }
      }
    },

    // 点击取消
    cancelFn() {
      this.$emit('input', false)
    },

    // 点击确定按钮
    addTagsFn() {
      this.$refs.form.validate(async valid => {
        if (!valid) return

        if (this.isEdit) {
          const res = await updateTags({ id: this.id, ...this.form })

          if (res?.data?.success) {
            this.$message.success('修改标签成功')
            this.$emit('successAdd')
          }
        } else {
          const res = await addTags({ ...this.form })
          if (res?.data?.id || res.data.id === 0) {
            this.$message.success('添加标签成功')
            this.$emit('successAdd')
          }
        }
      })
    },
    // 获取学科列表
    async subjectList() {
      const res = await subjectList()
      this.subjectlist = res.data
    }

  }
}
</script>

<style scoped>
.selectForm {
  width: 100%;
}
.btnFrom {
  float: right;
}

.diagForm {
  margin-bottom: 60px;
}
</style>
